// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <nebula/types/data_type.h>

namespace nebula {

    /// \brief EXPERIMENTAL: Container for a type pointer which can hold a
    /// dynamically created shared_ptr<DataType> if it needs to.
    struct TURBO_EXPORT TypeHolder {
        const DataType *type = nullptr;
        std::shared_ptr<DataType> owned_type;

        TypeHolder() = default;

        TypeHolder(const TypeHolder &other) = default;

        TypeHolder &operator=(const TypeHolder &other) = default;

        TypeHolder(TypeHolder &&other) = default;

        TypeHolder &operator=(TypeHolder &&other) = default;

        TypeHolder(std::shared_ptr<DataType> owned_type)  // NOLINT implicit construction
                : type(owned_type.get()), owned_type(std::move(owned_type)) {}

        TypeHolder(const DataType *type)  // NOLINT implicit construction
                : type(type) {}

        Type::type id() const { return this->type->id(); }

        std::shared_ptr<DataType> get_shared_ptr() const {
            return this->type != nullptr ? this->type->get_shared_ptr() : nullptr;
        }

        const DataType &operator*() const { return *this->type; }

        operator bool() const { return this->type != nullptr; }

        bool operator==(const TypeHolder &other) const {
            if (type == other.type) return true;
            if (type == nullptr || other.type == nullptr) return false;
            return type->equals(*other.type);
        }

        bool operator==(decltype(nullptr)) const { return this->type == nullptr; }

        bool operator==(const DataType &other) const {
            if (this->type == nullptr) return false;
            return other.equals(*this->type);
        }

        bool operator!=(const DataType &other) const { return !(*this == other); }

        bool operator==(const std::shared_ptr<DataType> &other) const {
            return *this == *other;
        }

        bool operator!=(const TypeHolder &other) const { return !(*this == other); }

        std::string to_string(bool show_metadata = false) const {
            return this->type ? this->type->to_string(show_metadata) : "<nullptr>";
        }

        static std::string to_string(const std::vector<TypeHolder> &, bool show_metadata = false);

        static std::vector<TypeHolder> FromTypes(
                const std::vector<std::shared_ptr<DataType>> &types);
    };

    TURBO_EXPORT
    std::ostream &operator<<(std::ostream &os, const TypeHolder &type);
}  // namespace nebula
